package models

import "gorm.io/gorm"

type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(30)"json:"username"`
	Password string `gorm:"type:varchar(225)"json:"password"`
}

type Userrolegxb struct {
	gorm.Model
	Uid int `gorm:"type:char(10)"json:"uid"`
	Rid int `gorm:"type:char(10)"json:"rid"`
}

type Role struct {
	gorm.Model
	Rolename string `gorm:"type:varchar(30)"json:"rolename"`
}

type Rolequangxb struct {
	gorm.Model
	Qid int `gorm:"type:char(10)"json:"qid"`
	Rid int `gorm:"type:char(10)"json:"rid"`
}

type Quan struct {
	gorm.Model
	Quanname string `gorm:"type:varchar(30)"json:"quanname"`
}

type Compan struct {
	gorm.Model
	Companname string `gorm:"type:varchar(30)"json:"companname"`
}

type Types struct {
	gorm.Model
	Typename string `gorm:"type:varchar(30)"json:"typename"`
}

type Orders struct {
	gorm.Model
	Ordersname string `gorm:"type:varchar(30)"json:"ordersname"`
	Cid        int    `gorm:"type:char(10)"json:"cid"`
	Tid        int    `gorm:"type:char(10)"json:"tid"`
}

func (*User) Dengs(username string, user *User) error {
	return DB.Where("username=?", username).Find(&user).Error
}
func (r *Rolequangxb) DataAdd(data *Rolequangxb) error {
	tx := DB.Begin()
	err := DB.Debug().Create(&data).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return err
}
func (r *Role) Quanlist(uid int) error {
	err := DB.Table("roles").
		Joins("LEFT JOIN userrolegxbs ON userrolegxbs.rid=roles.id").
		Where("userrolegxbs.uid=?", uid).
		Find(&r).Error
	return err
}
func (q *Quan) QuanLists(rid int) error {
	err := DB.Table("quans").
		Joins("LEFT JOIN rolequangxbs on rolequangxbs.qid=quans.id").
		Where("rolequangxbs.rid=?", rid).
		Find(&q).Error
	return err
}
func (c *Compan) CompanAdds(data *Compan) error {
	tx := DB.Begin()
	err := DB.Debug().Create(&data).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return err
}
func (r *Orders) OrdersAdds(data *Orders) error {
	tx := DB.Begin()
	err := DB.Debug().Create(&data).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return err
}
